//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.highglass.videoplayer.subTitle;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.highglass.videoplayer.subTitle.autodetect.AutoDetectUtil;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class SubDecoder extends SubTitleDecoder {
    private static int SUB_NK = 10240;
    private VobSub_Data_S vobsub_ds = new VobSub_Data_S();
    private Bitmap mVobBitmap = null;
    private int VobSubIndex = 0;
    private int VobSubStartTime = 0;
    private int VobSubEndTime = 0;
    private int VobSubFilePos = 0;
    private int langidx = 0;
    private int[] langLine = new int[21];
    private int langCount = 0;
    private int langLineCount = 0;

    public SubDecoder() {
    }

    private int sub_atoi_16x(char[] ptr, int len) {
        int ret = 0;
        boolean data_temp = false;

        for(int i = 0; i < len; ++i) {
            char c = ptr[i];
            int var7;
            if(c >= 48 && c <= 57) {
                var7 = c - 48 & 15;
                ret = ret << 4 | var7;
            }

            if(c >= 97 && c <= 102) {
                var7 = 10 + (c - 97) & 15;
                ret = ret << 4 | var7;
            }

            if(c >= 65 && c <= 70) {
                var7 = 10 + (c - 65) & 15;
                ret = ret << 4 | var7;
            }
        }

        return ret;
    }

    private void getPalette(String strLine) {
        boolean i = false;
        String[] strPilt = (String[])null;
        strPilt = strLine.split(",");
        strPilt[0] = strPilt[0].substring(9, strPilt[0].length());

        int var4;
        for(var4 = 1; var4 < 16; ++var4) {
            strPilt[var4] = strPilt[var4].substring(1, strPilt[var4].length());
        }

        for(var4 = 0; var4 < 16; ++var4) {
            this.vobsub_ds.sub_palette[var4] = this.sub_atoi_16x(strPilt[var4].toCharArray(), strPilt[var4].length());
        }

    }

    private void getVobSubStartTimeAndFilePose(String strLine) {
        String[] strPilt = (String[])null;
        strPilt = strLine.split(":");
        String strHour = strPilt[1].substring(1, strPilt[1].length());
        String strMinute = strPilt[2];
        String strSecond = strPilt[3];
        String strMicosecond = strPilt[4].substring(0, 3);
        String strFilePos = strPilt[5].substring(1, strPilt[5].length());
        this.VobSubFilePos = this.sub_atoi_16x(strFilePos.toCharArray(), strFilePos.length());
        int hour = Integer.parseInt(strHour);
        int minute = Integer.parseInt(strMinute);
        int second = Integer.parseInt(strSecond);
        int micosecond = Integer.parseInt(strMicosecond);
        this.VobSubStartTime = hour * 3600000 + minute * '\uea60' + second * 1000 + micosecond;
    }

    public void SubLoadMutiLine_VOBSUB(long vobSubTitlePos, String subFilePath) {
        boolean off = false;
        boolean i = false;
        RandomAccessFile randomFile = null;

        try {
            randomFile = new RandomAccessFile(subFilePath, "r");
            randomFile.seek(vobSubTitlePos);
            byte[] e = new byte[SUB_NK];
            if(randomFile.read(e) != -1) {
                Log.v("+++", "read " + SUB_NK + "bytes from one subtitle");
                int off1 = this.AssemblePacket(e);
                this.vobsub_getline(e, off1);
            }
        } catch (IOException var16) {
            var16.printStackTrace();
        } finally {
            if(randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException var15) {
                    ;
                }
            }

        }

    }

    private int AssemblePacket(byte[] bytesReadAhead) {
        boolean offset = false;
        int sumLen = 0;
        boolean streamId = false;
        int packets = 1;
        boolean iPackets = false;
        int[] nextPackPos = new int[5];
        int code = bytesReadAhead[14] | bytesReadAhead[15] << 8 | bytesReadAhead[16] << 16 | bytesReadAhead[17] << 24;
        int flag = bytesReadAhead[0] | bytesReadAhead[1] << 8 | bytesReadAhead[2] << 16 | bytesReadAhead[3] << 24;
        if(flag == -1174339584 && code == -1124007936 && (bytesReadAhead[21] & 128) != 0 && (bytesReadAhead[23] & 240) == 32 && (bytesReadAhead[bytesReadAhead[22] + 23] & 224) == 32) {
            VobSubHead subhead = this.vobsub_GetHead(bytesReadAhead, 0);
            int var14 = subhead.headLen;
            if(subhead.packetLen1 >= 2028) {
                int var15 = subhead.id;
                int i = 1;
                int j = SUB_NK >> 11;

                while(i < j) {
                    subhead = this.vobsub_GetHead(bytesReadAhead, i << 11);
                    if(subhead.id != var15) {
                        ++i;
                    } else {
                        if(subhead.bFirstPacket) {
                            break;
                        }

                        nextPackPos[packets] = i;
                        ++packets;
                        if(subhead.packetLen1 < 2028) {
                            break;
                        }

                        ++i;
                    }
                }
            }

            for(int var16 = 1; var16 < packets; ++var16) {
                subhead = this.vobsub_GetHead(bytesReadAhead, nextPackPos[var16] << 11);

                for(int k = 0; k < 2048 - subhead.headLen; ++k) {
                    bytesReadAhead[(var16 << 11) - sumLen + k] = bytesReadAhead[(nextPackPos[var16] << 11) + subhead.headLen + k];
                }

                sumLen += subhead.headLen;
            }

            return var14;
        } else {
            Log.v("+++", "The SubTitle is not the mark of  PS Packet and PES Packet");
            return -1;
        }
    }

    private VobSubHead vobsub_GetHead(byte[] bytesReadAhead, int filepos) {
        VobSubHead head = new VobSubHead();
        head.bFirstPacket = false;
        head.id = 0;
        head.iLang = 0;
        head.headLen = 0;
        head.packetLen1 = 0;
        head.packetLen2 = 0;
        head.dataLen = 0;
        head.headLen = bytesReadAhead[filepos + 22] + 24;
        head.packetLen1 = (bytesReadAhead[filepos + 18] & 255) << 8 | bytesReadAhead[filepos + 18 + 1] & 255;
        head.packetLen2 = (bytesReadAhead[filepos + head.headLen] & 255) << 8 | bytesReadAhead[filepos + head.headLen + 1] & 255;
        head.dataLen = (bytesReadAhead[filepos + head.headLen + 2] & 255) << 8 | bytesReadAhead[filepos + head.headLen + 3] & 255;
        head.id = bytesReadAhead[filepos + (bytesReadAhead[filepos + 22] & 255) + 23] & 255;
        head.iLang = head.id & 31;
        if(128 == (bytesReadAhead[filepos + 21] & 255 & 128)) {
            head.bFirstPacket = true;
        } else {
            head.bFirstPacket = false;
        }

        return head;
    }

    private void vobsub_getline(byte[] bytesReadAhead, int fileoffset) {
        int x = 0;
        int y = 0;
        boolean end0 = false;
        boolean end1 = false;
        byte blankLine = 0;
        int pixelCounbt = 0;
        short moderator = 550;
        boolean isFirstLine = false;
        boolean lastY = false;
        if(this.vobsub_getcom(bytesReadAhead, fileoffset) == -1) {
            ;
        }

        if(this.vobsub_ds.sub_ysize > 2) {
            this.vobsub_ds.sub_ysize -= 2;
        }

        int i = this.vobsub_ds.f_offset[1];
        this.vobsub_ds.fAligned = 0;
        byte[][] writebuffer = new byte[this.vobsub_ds.sub_ysize][this.vobsub_ds.sub_xsize];
        byte[] oneLineBuf = new byte[this.vobsub_ds.sub_xsize];

        int k;
        while(this.vobsub_ds.f_offset[0] < i && this.vobsub_ds.f_offset[1] / 2 < this.vobsub_ds.control_start && y < this.vobsub_ds.sub_ysize) {
            boolean rle = false;
            int var27 = this.GetNibble(bytesReadAhead, fileoffset);
            if(var27 < 4) {
                var27 = var27 << 4 | this.GetNibble(bytesReadAhead, fileoffset);
                if(var27 < 16) {
                    var27 = var27 << 4 | this.GetNibble(bytesReadAhead, fileoffset);
                    if(var27 < 64) {
                        var27 = var27 << 4 | this.GetNibble(bytesReadAhead, fileoffset);
                        if(var27 < 4) {
                            var27 |= this.vobsub_ds.sub_xsize - x << 2;
                        } else if(var27 < 256 && var27 > 15) {
                            continue;
                        }
                    }
                }
            }

            int destBuf = var27 & 3;
            int Bmp888Head = var27 >> 2;
            if(Bmp888Head != this.vobsub_ds.sub_xsize && Bmp888Head != 0 && !isFirstLine) {
                isFirstLine = true;
            }

            if(x == 0 && Bmp888Head >= this.vobsub_ds.sub_xsize) {
                ++blankLine;
                if(blankLine > 2) {
                    this.NextLine();
                    continue;
                }
            } else {
                blankLine = 0;
            }

            if(Bmp888Head > this.vobsub_ds.sub_xsize - x || Bmp888Head == 0) {
                Bmp888Head = this.vobsub_ds.sub_xsize - x;
            }

            for(k = 0; k < Bmp888Head && k + x <= this.vobsub_ds.sub_xsize - 1; ++k) {
                oneLineBuf[k + x] = (byte)(this.vobsub_ds.sub_alpha_bit[destBuf] & this.vobsub_ds.sub_color_map[destBuf]);
                ++pixelCounbt;
            }

            if(isFirstLine) {
                ;
            }

            x += Bmp888Head;
            if(x >= this.vobsub_ds.sub_xsize) {
                this.NextLine();
                x = 0;
                int p = 0;

                for(int w = moderator; w < this.vobsub_ds.sub_xsize; ++w) {
                    writebuffer[y][p] = oneLineBuf[w];
                    ++p;
                }

                for(int q = 0; q < moderator; ++q) {
                    writebuffer[y][p] = oneLineBuf[q];
                    ++p;
                }

                ++y;
            }
        }

        this.vobsub_ds.sub_ysize = y;
        byte[] var25 = new byte[118];
        this.CreateBmpHead888(this.vobsub_ds.sub_xsize, this.vobsub_ds.sub_ysize, var25);
        byte[] var26 = new byte[pixelCounbt + 118];

        for(k = 0; k < 118; ++k) {
            var26[k] = var25[k];
        }

        int t = 118;

        for(int m = this.vobsub_ds.sub_ysize - 1; m >= 0; --m) {
            for(int n = 0; n <= this.vobsub_ds.sub_xsize - 1; ++n) {
                var26[t] = writebuffer[m][n];
                ++t;
            }
        }

        this.mVobBitmap = BitmapFactory.decodeByteArray(var26, 0, pixelCounbt + 118);
        Log.v("+++", "mVobBitmap width is " + this.mVobBitmap.getWidth() + " mVobBitmap height is " + this.mVobBitmap.getHeight());
    }

    private int vobsub_getcom(byte[] bytesReadAhead, int filepos) {
        int start_off = 0;
        int[] f_offset = new int[2];
        boolean start_col = false;
        boolean end_col = false;
        boolean start_row = false;
        boolean end_row = false;
        boolean width = false;
        boolean height = false;
        int control_start = (bytesReadAhead[filepos + 2] & 255) << 8 | bytesReadAhead[filepos + 2 + 1] & 255;
        int next_off = control_start;
        this.vobsub_ds.control_start = control_start << 1;

        while(start_off != next_off) {
            start_off = next_off;
            int date = (bytesReadAhead[filepos + next_off] & 255) << 8 | bytesReadAhead[filepos + next_off + 1] & 255;
            next_off = (bytesReadAhead[filepos + next_off + 2] & 255) << 8 | bytesReadAhead[filepos + next_off + 3] & 255;
            int off = start_off + 4;

            for(int type = bytesReadAhead[filepos + off++] & 255; type != 255; type = bytesReadAhead[filepos + off++] & 255) {
                switch(type) {
                    case 0:
                    default:
                        break;
                    case 1:
                        this.vobsub_ds.sub_disp = true;
                        break;
                    case 2:
                        this.vobsub_ds.sub_time = 11 * date;
                        this.VobSubEndTime = 11 * date;
                        break;
                    case 3:
                        this.vobsub_ds.sub_color_map[3] = (bytesReadAhead[filepos + off] & 255) >> 4 & 15;
                        this.vobsub_ds.sub_color_map[2] = bytesReadAhead[filepos + off] & 255 & 15;
                        this.vobsub_ds.sub_color_map[1] = (bytesReadAhead[filepos + off + 1] & 255) >> 4 & 15;
                        this.vobsub_ds.sub_color_map[0] = bytesReadAhead[filepos + off + 1] & 255 & 15;
                        off += 2;
                        break;
                    case 4:
                        if(((bytesReadAhead[filepos + off] & 255) >> 4 & 15) > 0) {
                            this.vobsub_ds.sub_alpha_bit[3] = 15;
                        } else {
                            this.vobsub_ds.sub_alpha_bit[3] = 0;
                        }

                        if((bytesReadAhead[filepos + off] & 255 & 15) > 0) {
                            this.vobsub_ds.sub_alpha_bit[2] = 15;
                        } else {
                            this.vobsub_ds.sub_alpha_bit[2] = 0;
                        }

                        if(((bytesReadAhead[filepos + off + 1] & 255) >> 4 & 15) > 0) {
                            this.vobsub_ds.sub_alpha_bit[1] = 15;
                        } else {
                            this.vobsub_ds.sub_alpha_bit[1] = 0;
                        }

                        if((bytesReadAhead[filepos + off + 1] & 255 & 15) > 0) {
                            this.vobsub_ds.sub_alpha_bit[0] = 15;
                        } else {
                            this.vobsub_ds.sub_alpha_bit[0] = 0;
                        }

                        off += 2;
                        break;
                    case 5:
                        int a = (bytesReadAhead[filepos + off] & 255) << 16 | (bytesReadAhead[filepos + off + 1] & 255) << 8 | bytesReadAhead[filepos + off + 2] & 255;
                        int b = (bytesReadAhead[filepos + off + 3] & 255) << 16 | (bytesReadAhead[filepos + off + 4] & 255) << 8 | bytesReadAhead[filepos + off + 5] & 255;
                        int var21 = a >> 12;
                        int var20 = a & 4095;
                        int var22 = b >> 12;
                        int var23 = b & 4095;
                        this.vobsub_ds.sub_xsize = var20 < var21?0:var20 - var21 + 1;
                        this.vobsub_ds.sub_ysize = var23 < var22?0:var23 - var22;
                        off += 6;
                        break;
                    case 6:
                        this.vobsub_ds.f_offset[0] = (bytesReadAhead[filepos + off] & 255) << 8 | bytesReadAhead[filepos + off + 1];
                        this.vobsub_ds.f_offset[1] = (bytesReadAhead[filepos + off + 2] & 255) << 8 | bytesReadAhead[filepos + off + 3] & 255;
                        this.vobsub_ds.f_offset[0] <<= 1;
                        this.vobsub_ds.f_offset[1] <<= 1;
                        off += 4;
                        break;
                    case 255:
                        return 0;
                }
            }
        }

        return 0;
    }

    int GetNibble(byte[] bytesReadAhead, int filepos) {
        int nibblep = this.vobsub_ds.f_offset[this.vobsub_ds.fAligned];
        int pos = nibblep >> 1;
        if(pos >= this.vobsub_ds.control_start) {
            return 0;
        } else {
            int nib = bytesReadAhead[filepos + pos] & 255;
            if((nibblep & 1) > 0) {
                nib &= 15;
            } else {
                nib >>= 4;
            }

            ++this.vobsub_ds.f_offset[this.vobsub_ds.fAligned];
            return nib;
        }
    }

    private void NextLine() {
        if((this.vobsub_ds.f_offset[this.vobsub_ds.fAligned] & 1) > 0) {
            ++this.vobsub_ds.f_offset[this.vobsub_ds.fAligned];
        }

        this.vobsub_ds.fAligned = this.vobsub_ds.fAligned + 1 & 1;
    }

    private void CreateBmpHead888(int width, int height, byte[] bmp) {
        BITMAPFILEHEADER1 BmpFileHead = new BITMAPFILEHEADER1();
        BITMAPINFOHEADER1 BmpInfoHead = new BITMAPINFOHEADER1();
        BmpInfoHead.biSize = 40;
        BmpInfoHead.biWidth = width;
        BmpInfoHead.biHeight = height;
        BmpInfoHead.biPlanes = 1;
        BmpInfoHead.biBitCount = 4;
        BmpInfoHead.biCompression = 0;
        BmpInfoHead.biSizeImage = 0;
        BmpInfoHead.biXPelsPerMeter = 0;
        BmpInfoHead.biYPelsPerMeter = 0;
        BmpInfoHead.biClrUsed = 16;
        BmpInfoHead.biClrImportant = 0;
        BmpFileHead.bfType = 19778;
        boolean a = true;
        boolean b = true;
        BmpFileHead.bfSize = 54 + BmpInfoHead.biSizeImage + 64;
        BmpFileHead.bfReserved1 = 0;
        BmpFileHead.bfReserved2 = 0;
        BmpFileHead.bfOffBits = 118;
        bmp[0] = (byte)(BmpFileHead.bfType & 255);
        bmp[1] = (byte)(BmpFileHead.bfType >> 8 & 255);
        bmp[2] = (byte)(BmpFileHead.bfSize & 255);
        bmp[3] = (byte)(BmpFileHead.bfSize >> 8 & 255);
        bmp[4] = (byte)(BmpFileHead.bfSize >> 16 & 255);
        bmp[5] = (byte)(BmpFileHead.bfSize >> 24 & 255);
        bmp[6] = (byte)(BmpFileHead.bfReserved1 & 255);
        bmp[7] = (byte)(BmpFileHead.bfReserved1 >> 8 & 255);
        bmp[8] = (byte)(BmpFileHead.bfReserved2 & 255);
        bmp[9] = (byte)(BmpFileHead.bfReserved2 >> 8 & 255);
        bmp[10] = (byte)(BmpFileHead.bfOffBits & 255);
        bmp[11] = (byte)(BmpFileHead.bfOffBits >> 8 & 255);
        bmp[12] = (byte)(BmpFileHead.bfOffBits >> 16 & 255);
        bmp[13] = (byte)(BmpFileHead.bfOffBits >> 24 & 255);
        bmp[14] = (byte)(BmpInfoHead.biSize & 255);
        bmp[15] = (byte)(BmpInfoHead.biSize >> 8 & 255);
        bmp[16] = (byte)(BmpInfoHead.biSize >> 16 & 255);
        bmp[17] = (byte)(BmpInfoHead.biSize >> 24 & 255);
        bmp[18] = (byte)(BmpInfoHead.biWidth & 255);
        bmp[19] = (byte)(BmpInfoHead.biWidth >> 8 & 255);
        bmp[20] = (byte)(BmpInfoHead.biWidth >> 16 & 255);
        bmp[21] = (byte)(BmpInfoHead.biWidth >> 24 & 255);
        bmp[22] = (byte)(BmpInfoHead.biHeight & 255);
        bmp[23] = (byte)(BmpInfoHead.biHeight >> 8 & 255);
        bmp[24] = (byte)(BmpInfoHead.biHeight >> 16 & 255);
        bmp[25] = (byte)(BmpInfoHead.biHeight >> 24 & 255);
        bmp[26] = (byte)(BmpInfoHead.biPlanes & 255);
        bmp[27] = (byte)(BmpInfoHead.biPlanes >> 8 & 255);
        bmp[28] = (byte)(BmpInfoHead.biBitCount & 255);
        bmp[29] = (byte)(BmpInfoHead.biBitCount >> 8 & 255);
        bmp[30] = (byte)(BmpInfoHead.biCompression & 255);
        bmp[31] = (byte)(BmpInfoHead.biCompression >> 8 & 255);
        bmp[32] = (byte)(BmpInfoHead.biCompression >> 16 & 255);
        bmp[33] = (byte)(BmpInfoHead.biCompression >> 24 & 255);
        bmp[34] = (byte)(BmpInfoHead.biSizeImage & 255);
        bmp[35] = (byte)(BmpInfoHead.biSizeImage >> 8 & 255);
        bmp[36] = (byte)(BmpInfoHead.biSizeImage >> 16 & 255);
        bmp[37] = (byte)(BmpInfoHead.biSizeImage >> 24 & 255);
        bmp[38] = (byte)(BmpInfoHead.biXPelsPerMeter & 255);
        bmp[39] = (byte)(BmpInfoHead.biXPelsPerMeter >> 8 & 255);
        bmp[40] = (byte)(BmpInfoHead.biXPelsPerMeter >> 16 & 255);
        bmp[41] = (byte)(BmpInfoHead.biXPelsPerMeter >> 24 & 255);
        bmp[42] = (byte)(BmpInfoHead.biYPelsPerMeter & 255);
        bmp[43] = (byte)(BmpInfoHead.biYPelsPerMeter >> 8 & 255);
        bmp[44] = (byte)(BmpInfoHead.biYPelsPerMeter >> 16 & 255);
        bmp[45] = (byte)(BmpInfoHead.biYPelsPerMeter >> 24 & 255);
        bmp[46] = (byte)(BmpInfoHead.biClrUsed & 255);
        bmp[47] = (byte)(BmpInfoHead.biClrUsed >> 8 & 255);
        bmp[48] = (byte)(BmpInfoHead.biClrUsed >> 16 & 255);
        bmp[49] = (byte)(BmpInfoHead.biClrUsed >> 24 & 255);
        bmp[50] = (byte)(BmpInfoHead.biClrImportant & 255);
        bmp[51] = (byte)(BmpInfoHead.biClrImportant >> 8 & 255);
        bmp[52] = (byte)(BmpInfoHead.biClrImportant >> 16 & 255);
        bmp[53] = (byte)(BmpInfoHead.biClrImportant >> 24 & 255);
        bmp[54] = 0;
        bmp[55] = 0;
        bmp[56] = 0;
        bmp[57] = 0;
        bmp[58] = 31;
        bmp[59] = 31;
        bmp[60] = 31;
        bmp[61] = 0;
        bmp[62] = -1;
        bmp[63] = -1;
        bmp[64] = -1;
        bmp[65] = 0;
        bmp[66] = 6;
        bmp[67] = 62;
        bmp[68] = -27;
        bmp[69] = 0;
        bmp[70] = 126;
        bmp[71] = 126;
        bmp[72] = 126;
        bmp[73] = 0;
        bmp[74] = -66;
        bmp[75] = -66;
        bmp[76] = -66;
        bmp[77] = 0;
        bmp[78] = -17;
        bmp[79] = -67;
        bmp[80] = 17;
        bmp[81] = 0;
        bmp[82] = -22;
        bmp[83] = 18;
        bmp[84] = -24;
        bmp[85] = 0;
        bmp[86] = 25;
        bmp[87] = -1;
        bmp[88] = -7;
        bmp[89] = 0;
        bmp[90] = 111;
        bmp[91] = 0;
        bmp[92] = 111;
        bmp[93] = 0;
        bmp[94] = 3;
        bmp[95] = 117;
        bmp[96] = 4;
        bmp[97] = 0;
        bmp[98] = 2;
        bmp[99] = 30;
        bmp[100] = 114;
        bmp[101] = 0;
        bmp[102] = 118;
        bmp[103] = 95;
        bmp[104] = 8;
        bmp[105] = 0;
        bmp[106] = 116;
        bmp[107] = 8;
        bmp[108] = 115;
        bmp[109] = 0;
        bmp[110] = 8;
        bmp[111] = -107;
        bmp[112] = 122;
        bmp[113] = 0;
        bmp[114] = -1;
        bmp[115] = -1;
        bmp[116] = -1;
        bmp[117] = 0;
    }

    private void CreateBmpHead565(int width, int height, byte[] bmp) {
        BITMAPFILEHEADER1 BmpFileHead = new BITMAPFILEHEADER1();
        BITMAPINFOHEADER1 BmpInfoHead = new BITMAPINFOHEADER1();
        int[] bmpdata_buffer = new int[3];
        int expendwidth = width * 2 + 3 & -4;
        BmpInfoHead.biSize = 40;
        BmpInfoHead.biWidth = width;
        BmpInfoHead.biHeight = height;
        BmpInfoHead.biPlanes = 1;
        BmpInfoHead.biBitCount = 16;
        BmpInfoHead.biCompression = 3;
        BmpInfoHead.biSizeImage = expendwidth * BmpInfoHead.biHeight;
        BmpInfoHead.biXPelsPerMeter = 0;
        BmpInfoHead.biYPelsPerMeter = 0;
        BmpInfoHead.biClrUsed = 16;
        BmpInfoHead.biClrImportant = 0;
        BmpFileHead.bfType = 19778;
        boolean a = true;
        boolean b = true;
        BmpFileHead.bfSize = 54 + BmpInfoHead.biSizeImage + 12 + 64;
        BmpFileHead.bfReserved1 = 0;
        BmpFileHead.bfReserved2 = 0;
        BmpFileHead.bfOffBits = 130;
        bmpdata_buffer[0] = '\uf800';
        bmpdata_buffer[1] = 2016;
        bmpdata_buffer[2] = 31;
        bmp[0] = (byte)(BmpFileHead.bfType & 255);
        bmp[1] = (byte)(BmpFileHead.bfType >> 8 & 255);
        bmp[2] = (byte)(BmpFileHead.bfSize & 255);
        bmp[3] = (byte)(BmpFileHead.bfSize >> 8 & 255);
        bmp[4] = (byte)(BmpFileHead.bfSize >> 16 & 255);
        bmp[5] = (byte)(BmpFileHead.bfSize >> 24 & 255);
        bmp[6] = (byte)(BmpFileHead.bfReserved1 & 255);
        bmp[7] = (byte)(BmpFileHead.bfReserved1 >> 8 & 255);
        bmp[8] = (byte)(BmpFileHead.bfReserved2 & 255);
        bmp[9] = (byte)(BmpFileHead.bfReserved2 >> 8 & 255);
        bmp[10] = (byte)(BmpFileHead.bfOffBits & 255);
        bmp[11] = (byte)(BmpFileHead.bfOffBits >> 8 & 255);
        bmp[12] = (byte)(BmpFileHead.bfOffBits >> 16 & 255);
        bmp[13] = (byte)(BmpFileHead.bfOffBits >> 24 & 255);
        bmp[14] = (byte)(BmpInfoHead.biSize & 255);
        bmp[15] = (byte)(BmpInfoHead.biSize >> 8 & 255);
        bmp[16] = (byte)(BmpInfoHead.biSize >> 16 & 255);
        bmp[17] = (byte)(BmpInfoHead.biSize >> 24 & 255);
        bmp[18] = (byte)(BmpInfoHead.biWidth & 255);
        bmp[19] = (byte)(BmpInfoHead.biWidth >> 8 & 255);
        bmp[20] = (byte)(BmpInfoHead.biWidth >> 16 & 255);
        bmp[21] = (byte)(BmpInfoHead.biWidth >> 24 & 255);
        bmp[22] = (byte)(BmpInfoHead.biHeight & 255);
        bmp[23] = (byte)(BmpInfoHead.biHeight >> 8 & 255);
        bmp[24] = (byte)(BmpInfoHead.biHeight >> 16 & 255);
        bmp[25] = (byte)(BmpInfoHead.biHeight >> 24 & 255);
        bmp[26] = (byte)(BmpInfoHead.biPlanes & 255);
        bmp[27] = (byte)(BmpInfoHead.biPlanes >> 8 & 255);
        bmp[28] = (byte)(BmpInfoHead.biBitCount & 255);
        bmp[29] = (byte)(BmpInfoHead.biBitCount >> 8 & 255);
        bmp[30] = (byte)(BmpInfoHead.biCompression & 255);
        bmp[31] = (byte)(BmpInfoHead.biCompression >> 8 & 255);
        bmp[32] = (byte)(BmpInfoHead.biCompression >> 16 & 255);
        bmp[33] = (byte)(BmpInfoHead.biCompression >> 24 & 255);
        bmp[34] = (byte)(BmpInfoHead.biSizeImage & 255);
        bmp[35] = (byte)(BmpInfoHead.biSizeImage >> 8 & 255);
        bmp[36] = (byte)(BmpInfoHead.biSizeImage >> 16 & 255);
        bmp[37] = (byte)(BmpInfoHead.biSizeImage >> 24 & 255);
        bmp[38] = (byte)(BmpInfoHead.biXPelsPerMeter & 255);
        bmp[39] = (byte)(BmpInfoHead.biXPelsPerMeter >> 8 & 255);
        bmp[40] = (byte)(BmpInfoHead.biXPelsPerMeter >> 16 & 255);
        bmp[41] = (byte)(BmpInfoHead.biXPelsPerMeter >> 24 & 255);
        bmp[42] = (byte)(BmpInfoHead.biYPelsPerMeter & 255);
        bmp[43] = (byte)(BmpInfoHead.biYPelsPerMeter >> 8 & 255);
        bmp[44] = (byte)(BmpInfoHead.biYPelsPerMeter >> 16 & 255);
        bmp[45] = (byte)(BmpInfoHead.biYPelsPerMeter >> 24 & 255);
        bmp[46] = (byte)(BmpInfoHead.biClrUsed & 255);
        bmp[47] = (byte)(BmpInfoHead.biClrUsed >> 8 & 255);
        bmp[48] = (byte)(BmpInfoHead.biClrUsed >> 16 & 255);
        bmp[49] = (byte)(BmpInfoHead.biClrUsed >> 24 & 255);
        bmp[50] = (byte)(BmpInfoHead.biClrImportant & 255);
        bmp[51] = (byte)(BmpInfoHead.biClrImportant >> 8 & 255);
        bmp[52] = (byte)(BmpInfoHead.biClrImportant >> 16 & 255);
        bmp[53] = (byte)(BmpInfoHead.biClrImportant >> 24 & 255);
        bmp[54] = 0;
        bmp[55] = -8;
        bmp[56] = 0;
        bmp[57] = 0;
        bmp[58] = 7;
        bmp[59] = -32;
        bmp[60] = 0;
        bmp[61] = 0;
        bmp[62] = 0;
        bmp[63] = 31;
        bmp[64] = 0;
        bmp[65] = 0;
        bmp[66] = 0;
        bmp[67] = 0;
        bmp[68] = 0;
        bmp[69] = 0;
        bmp[70] = 31;
        bmp[71] = 31;
        bmp[72] = 31;
        bmp[73] = 0;
        bmp[74] = -1;
        bmp[75] = -1;
        bmp[76] = -1;
        bmp[77] = 0;
        bmp[78] = -27;
        bmp[79] = 62;
        bmp[80] = 6;
        bmp[81] = 0;
        bmp[82] = 126;
        bmp[83] = 126;
        bmp[84] = 126;
        bmp[85] = 0;
        bmp[86] = -66;
        bmp[87] = -66;
        bmp[88] = -66;
        bmp[89] = 0;
        bmp[90] = 17;
        bmp[91] = -67;
        bmp[92] = -17;
        bmp[93] = 0;
        bmp[94] = -24;
        bmp[95] = 18;
        bmp[96] = -22;
        bmp[97] = 0;
        bmp[98] = -7;
        bmp[99] = -1;
        bmp[100] = 25;
        bmp[101] = 0;
        bmp[102] = 0;
        bmp[103] = 0;
        bmp[104] = 111;
        bmp[105] = 0;
        bmp[106] = 4;
        bmp[107] = 117;
        bmp[108] = 3;
        bmp[109] = 0;
        bmp[110] = 114;
        bmp[111] = 30;
        bmp[112] = 2;
        bmp[113] = 0;
        bmp[114] = 8;
        bmp[115] = 95;
        bmp[116] = 118;
        bmp[117] = 0;
        bmp[118] = 115;
        bmp[119] = 8;
        bmp[120] = 116;
        bmp[121] = 0;
        bmp[122] = 122;
        bmp[123] = -107;
        bmp[124] = 8;
        bmp[125] = 0;
        bmp[126] = -1;
        bmp[127] = -1;
        bmp[128] = -1;
        bmp[129] = 0;
    }

    private int getSubTime(String str) {
        String[] timeArray = str.split(":");
        int hour = Integer.valueOf(timeArray[0]).intValue();
        int minute = Integer.valueOf(timeArray[1]).intValue();
        int second = Integer.valueOf(timeArray[2]).intValue();
        int millsecond = Integer.valueOf(timeArray[3]).intValue();
        return hour * 3600000 + minute * '\uea60' + second * 1000 + millsecond;
    }

    public void saveMyBitmap(Bitmap mBitmap, String filePath) throws IOException {
        File f = new File(filePath);
        f.createNewFile();
        FileOutputStream fOut = null;

        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException var8) {
            var8.printStackTrace();
        }

        mBitmap.compress(CompressFormat.PNG, 100, fOut);

        try {
            fOut.flush();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        try {
            fOut.close();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

    }

    public Map<String, List<SubTitleContent>> decodeSubTitle(String subTitlePath, String encode) throws Exception {
        Log.v("+++", ">>>>>>>>>>>>>>>>>>>>>>>decode idx file " + subTitlePath);
        FileInputStream in = new FileInputStream(subTitlePath);
        FileInputStream encodeinput = new FileInputStream(subTitlePath);
        BufferedInputStream encodebin = new BufferedInputStream(encodeinput);
        BufferedInputStream bin = new BufferedInputStream(in);
        String encodeType = AutoDetectUtil.detectEncode(6, encodebin);

        try {
            encodebin.close();
        } catch (Exception var23) {
            var23.printStackTrace();
        }

        try {
            encodeinput.close();
        } catch (Exception var22) {
            var22.printStackTrace();
        }

        Log.v("+++", ">>>>>>>>>>>>>>>>>>>>>>>encodeType: " + encodeType);
        InputStreamReader isr = null;
        if(encodeType != null && !"ascii".equals(encodeType)) {
            if("windows-1252".equalsIgnoreCase(encodeType)) {
                encodeType = "Unicode";
            }

            isr = new InputStreamReader(bin, encodeType);
        } else {
            isr = new InputStreamReader(bin);
        }

        BufferedReader br = new BufferedReader(isr);
        TreeMap subTitleMap = new TreeMap();
        TreeMap langMap = new TreeMap();
        SubTitleContent content = null;
        String subTitleLine = null;
        String langStr = null;
        String timeStr = null;
        boolean time = false;
        String posStr = null;
        boolean pos = false;
        int langIndexCount = 0;
        boolean langIndex = false;
        int indexCount = 0;

        while((subTitleLine = br.readLine()) != null) {
            subTitleLine = subTitleLine.trim();
            if(subTitleLine.startsWith("id:")) {
                int var26 = Integer.valueOf(subTitleLine.split(":")[subTitleLine.split(":").length - 1].trim()).intValue();
                if(langStr != null && langMap.get(langStr) != null) {
                    ((langStruct)langMap.get(langStr)).setLangEndLine(var26 - 1);
                }

                langStr = subTitleLine.split(",")[0].split(":")[1].trim();
                langMap.put(langStr, new langStruct(langIndexCount++, var26, 2147483647));
                subTitleMap.put(langStr, new ArrayList());
            }

            if(subTitleLine.startsWith("timestamp:")) {
                timeStr = subTitleLine.split(",")[0].trim().split(" ")[1].trim();
                int var24 = this.getSubTime(timeStr);
                posStr = subTitleLine.split("filepos:")[1].trim();
                int var25 = Integer.parseInt(posStr, 16);
                this.SubLoadMutiLine_VOBSUB((long)var25, subTitlePath.replaceAll(".idx", ".sub"));
                content = new SubTitleContent();
                content.setmSubTitleIndex(indexCount++);
                content.setmLanguageClass(langStr);
                content.setmFilepos(var25);
                content.setmSubTitleStartTime(var24);
                content.setmSubTitleEndTime(var24 + this.VobSubEndTime);
                content.setmSubTitleBmp(this.mVobBitmap);
                this.saveMyBitmap(this.mVobBitmap, "/flash/" + content.hashCode() + ".png");
                this.mVobBitmap.recycle();
                content.setmSubTitleLine((String)null);
                ((List)subTitleMap.get(langStr)).add(content);
            }
        }

        return subTitleMap;
    }

    class BITMAPFILEHEADER1 {
        short bfType;
        int bfSize;
        short bfReserved1;
        short bfReserved2;
        int bfOffBits;

        BITMAPFILEHEADER1() {
        }
    }

    class BITMAPINFOHEADER1 {
        int biSize;
        int biWidth;
        int biHeight;
        short biPlanes;
        short biBitCount;
        int biCompression;
        int biSizeImage;
        int biXPelsPerMeter;
        int biYPelsPerMeter;
        int biClrUsed;
        int biClrImportant;

        BITMAPINFOHEADER1() {
        }
    }

    class VobSubHead {
        boolean bFirstPacket;
        int id;
        int iLang;
        int headLen;
        int packetLen1;
        int packetLen2;
        int dataLen;

        VobSubHead() {
        }
    }

    class VobSub_Data_S {
        int sub_xsize;
        int sub_ysize;
        int[] f_offset = new int[2];
        int fAligned;
        boolean sub_disp;
        int[] sub_color_map = new int[4];
        int[] sub_alpha_bit = new int[4];
        int sub_time;
        int control_start;
        int[] sub_palette = new int[16];

        VobSub_Data_S() {
        }
    }

    class langStruct {
        int langIndex = 0;
        int langStartLine = 0;
        int langEndLine = 0;

        public langStruct(int langIndex, int langStartLine, int langEndLine) {
            this.langIndex = langIndex;
            this.langStartLine = langStartLine;
            this.langEndLine = langEndLine;
        }

        public int getLangIndex() {
            return this.langIndex;
        }

        public int getLangStartLine() {
            return this.langStartLine;
        }

        public int getLangEndLine() {
            return this.langEndLine;
        }

        public void setLangIndex(int langIndex) {
            this.langIndex = langIndex;
        }

        public void setLangStartLine(int langStartLine) {
            this.langStartLine = langStartLine;
        }

        public void setLangEndLine(int langEndLine) {
            this.langEndLine = langEndLine;
        }
    }
}
